﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;

namespace Atlantyda
{
    public class AimlBot
    {
        private string[] welcomes = new string[3] { "Witaj", "Cześć", "Siema!" };
        private string[][] questions = new string[][] { new string[] { "Gdzie jest najbliższe pole z energią?", "Daleko do pola z energią?", "Powiedź mi gdzie jest energia?" }, new string[] { "Widziałeś przeszkode?", "Znasz pozycje jakieś przeszkody?", "Gadaj gdzie są przeszkody!" } };
        private string[][] answers = new string[][] { new string[] { "Jest na pozycji {0},{1}", "Widziałem na pozycji {0},{1}" }, new string[] { "Nie powiem", "Nie mam zielonego pojęcia", "Nie udzielam informacji obcym osobom" } };
        private string[] goodbyes = new string[3] { "Powodzenia", "Narazie", "Trzymaj się!" };
        private string[] thanks = new string[3] { "Dzięki", "Dziękuje", "Jenkins!" };
        private string last_caller_name;
        private string last_question_asked;
        private string end_conversation;

        public string EndConversation
        {
            get { return end_conversation; }
            set { end_conversation = value; }
        }

        public string LastCallerName
        {
            get { return last_caller_name; }
            set { last_caller_name = value; }
        }

        public void StartConversation(string agent_name)
        {
            if (agent_name + "." + Agent.GroupName != EndConversation)
            {
                Agent.locker = true;
                last_caller_name = agent_name + "." + Agent.GroupName;
                Random rand = new Random();
                last_question_asked = welcomes[rand.Next(2)];
                Agent.Speak(last_question_asked);
                ReleaseAim();
            }
            else
                Agent.locker = false;
        }

        public void Chat(string sender, string message)
        {
            Random rand = new Random();
            if (last_caller_name == sender)
            {
                if (welcomes.Contains(message) && welcomes.Contains(last_question_asked))
                {
                    last_question_asked = questions[rand.Next(2)][rand.Next(2)];
                    Agent.Speak(last_question_asked);
                }
                else if (questions[0].Contains(last_question_asked))
                {
                    //todo otrzymanie informacji o energii
                    Agent.Speak(thanks[rand.Next(2)]);
                    Agent.Speak(goodbyes[rand.Next(2)]);
                    EndConversation = last_caller_name;
                    last_caller_name = null;
                    last_question_asked = null;
                    Agent.locker = false;
                }
                else if (questions[1].Contains(last_question_asked))
                {
                    //todo otrzymanie informacji o przeszkodzie
                    Agent.Speak(thanks[rand.Next(2)]);
                    Agent.Speak(goodbyes[rand.Next(2)]);
                    EndConversation = last_caller_name;
                    last_caller_name = null;
                    last_question_asked = null;
                    Agent.locker = false;
                }
                else if (questions[0].Contains(message))
                {
                    last_caller_name = sender;
                    int x, y;
                    if (Agent.AgentMap.FindEnergy(out x, out y))
                        Agent.Speak(String.Format(answers[0][rand.Next(1)], x, y));
                    else
                        Agent.Speak(answers[1][rand.Next(2)]);
                }
                else if (questions[1].Contains(message))
                {
                    last_caller_name = sender;
                    int x, y;
                    if (Agent.AgentMap.FindObstacle(out x, out y))
                        Agent.Speak(String.Format(answers[0][rand.Next(1)], x, y));
                    else
                        Agent.Speak(answers[1][rand.Next(2)]);
                }
                else if (goodbyes.Contains(message))
                {
                    EndConversation = sender;
                    last_caller_name = null;
                    last_question_asked = null;
                    Agent.Speak(goodbyes[rand.Next(2)]);
                    Agent.locker = false;
                }
            }
            else if (last_caller_name == null)
            {
                last_caller_name = sender;
                last_question_asked = welcomes[rand.Next(2)];
                Agent.Speak(last_question_asked);
            }
            ReleaseAim();
        }

        public void ReleaseAim()
        {
            Thread.Sleep(3000);
            if (welcomes.Contains(last_question_asked))
            {
                last_caller_name = null;
                last_question_asked = null;
                Agent.locker = false;
            }
        }
    }
}
